//
//  TextFieldEffectsExample.swift
//  SwiftCode
//
//  Created by yuhus on 2023/7/24.
//

import Foundation
import UIKit
import SnapKit

final class TextFieldEffectsExample: ListViewController {
  override func getItemCount() -> Int {
    data.count
  }
  
  override func getItem(_ indexPath: IndexPath) -> Item {
    data[indexPath.row]
  }
  
  private lazy var data: [Item] = [
    .init(title: "AkiraTextField", description: "", type: AkiraTextFieldExample.self),
    .init(title: "MadokaTextField", description: "", type: MadokaTextFieldExample.self),
    .init(title: "DroidTextField", description: "", type: DroidTextFieldExample.self)
  ]
}

private final class AkiraTextFieldExample: TextFieldEffects {
  override func getTextFieldEffects() -> UITextField {
    let textField = AkiraTextField()
    textField.placeholder = "Last name"
    textField.backgroundColor = .red.withAlphaComponent(0.1)
    
    textField.borderColor = .red
    textField.font = .systemFont(ofSize: 24)
    return textField
  }
}

private final class MadokaTextFieldExample: TextFieldEffects {
  override func getTextFieldEffects() -> UITextField {
    let textField = MadokaTextField()
    textField.placeholder = "Last name"
    textField.backgroundColor = .red.withAlphaComponent(0.1)
    
    textField.borderColor = .red
    textField.font = .systemFont(ofSize: 24)
    textField.text = "July 26, 2005"
    return textField
  }
}

private final class DroidTextFieldExample: TextFieldEffects {
  override func getTextFieldEffects() -> UITextField {
    let textField = DroidTextField()
    textField.placeholder = "Last name"
    textField.backgroundColor = .red.withAlphaComponent(0.1)
    textField.placeholderLabel.font = .systemFont(ofSize: 15)
    
//    textField.borderColor = .red
    textField.font = .systemFont(ofSize: 15, weight: .semibold)
    textField.clearButtonMode = .whileEditing
    textField.text = "July 26, 2005"
    return textField
  }
}

open class TextFieldEffects: UIViewController {
  open override func viewDidLoad() {
    super.viewDidLoad()
    
    view.backgroundColor = .white
    view.addSubview(textField)
    
    textField.snp.makeConstraints { make in
      make.height.equalTo(48)
      make.top.equalTo(view.safeAreaLayoutGuide).offset(16)
      make.leading.trailing.equalToSuperview().inset(16)
    }
    let textFieldEffects = getTextFieldEffects()
    view.addSubview(textFieldEffects)
    textFieldEffects.snp.makeConstraints { make in
      make.height.equalTo(72)
      make.top.equalTo(textField.snp.bottom).offset(24)
      make.leading.trailing.equalToSuperview().inset(16)
    }
  }
  
  open func getTextFieldEffects() -> UITextField {
    fatalError("\(#function) must be overridden")
  }
  
  private lazy var textField: UITextField = {
    let textField = UITextField()
    textField.placeholder = "First name"
    textField.backgroundColor = .red.withAlphaComponent(0.1)
    
    textField.borderStyle = .none
    return textField
  }()
}
